1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.primitives;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkElementIndex;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkPositionIndexes;
23
24 import com.google.common.annotations.Beta;
25 import com.google.common.annotations.GwtCompatible;
26
27 import java.io.Serializable;
28 import java.util.AbstractList;
29 import java.util.Arrays;
30 import java.util.BitSet;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.List;
35 import java.util.RandomAccess;
36
37
38
39
40
41
42
43
44
45
46
47
48 @GwtCompatible
49 public final class Booleans {
50 private Booleans() {}
51
52
53
54
55
56
57
58
59 public static int hashCode(boolean value) {
60 return value ? 1231 : 1237;
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public static int compare(boolean a, boolean b) {
77 return (a == b) ? 0 : (a ? 1 : -1);
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public static boolean contains(boolean[] array, boolean target) {
95 for (boolean value : array) {
96 if (value == target) {
97 return true;
98 }
99 }
100 return false;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public static int indexOf(boolean[] array, boolean target) {
117 return indexOf(array, target, 0, array.length);
118 }
119
120
121 private static int indexOf(
122 boolean[] array, boolean target, int start, int end) {
123 for (int i = start; i < end; i++) {
124 if (array[i] == target) {
125 return i;
126 }
127 }
128 return -1;
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142 public static int indexOf(boolean[] array, boolean[] target) {
143 checkNotNull(array, "array");
144 checkNotNull(target, "target");
145 if (target.length == 0) {
146 return 0;
147 }
148
149 outer:
150 for (int i = 0; i < array.length - target.length + 1; i++) {
151 for (int j = 0; j < target.length; j++) {
152 if (array[i + j] != target[j]) {
153 continue outer;
154 }
155 }
156 return i;
157 }
158 return -1;
159 }
160
161
162
163
164
165
166
167
168
169
170 public static int lastIndexOf(boolean[] array, boolean target) {
171 return lastIndexOf(array, target, 0, array.length);
172 }
173
174
175 private static int lastIndexOf(
176 boolean[] array, boolean target, int start, int end) {
177 for (int i = end - 1; i >= start; i--) {
178 if (array[i] == target) {
179 return i;
180 }
181 }
182 return -1;
183 }
184
185
186
187
188
189
190
191
192
193
194 public static boolean[] concat(boolean[]... arrays) {
195 int length = 0;
196 for (boolean[] array : arrays) {
197 length += array.length;
198 }
199 boolean[] result = new boolean[length];
200 int pos = 0;
201 for (boolean[] array : arrays) {
202 System.arraycopy(array, 0, result, pos, array.length);
203 pos += array.length;
204 }
205 return result;
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 public static boolean[] ensureCapacity(
225 boolean[] array, int minLength, int padding) {
226 checkArgument(minLength >= 0, "Invalid minLength: %s", minLength);
227 checkArgument(padding >= 0, "Invalid padding: %s", padding);
228 return (array.length < minLength)
229 ? copyOf(array, minLength + padding)
230 : array;
231 }
232
233
234 private static boolean[] copyOf(boolean[] original, int length) {
235 boolean[] copy = new boolean[length];
236 System.arraycopy(original, 0, copy, 0, Math.min(original.length, length));
237 return copy;
238 }
239
240
241
242
243
244
245
246
247
248
249 public static String join(String separator, boolean... array) {
250 checkNotNull(separator);
251 if (array.length == 0) {
252 return "";
253 }
254
255
256 StringBuilder builder = new StringBuilder(array.length * 7);
257 builder.append(array[0]);
258 for (int i = 1; i < array.length; i++) {
259 builder.append(separator).append(array[i]);
260 }
261 return builder.toString();
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 public static Comparator<boolean[]> lexicographicalComparator() {
281 return LexicographicalComparator.INSTANCE;
282 }
283
284 private enum LexicographicalComparator implements Comparator<boolean[]> {
285 INSTANCE;
286
287 @Override
288 public int compare(boolean[] left, boolean[] right) {
289 int minLength = Math.min(left.length, right.length);
290 for (int i = 0; i < minLength; i++) {
291 int result = Booleans.compare(left[i], right[i]);
292 if (result != 0) {
293 return result;
294 }
295 }
296 return left.length - right.length;
297 }
298 }
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317 public static boolean[] toArray(Collection<Boolean> collection) {
318 if (collection instanceof BooleanArrayAsList) {
319 return ((BooleanArrayAsList) collection).toBooleanArray();
320 }
321
322 Object[] boxedArray = collection.toArray();
323 int len = boxedArray.length;
324 boolean[] array = new boolean[len];
325 for (int i = 0; i < len; i++) {
326
327 array[i] = (Boolean) checkNotNull(boxedArray[i]);
328 }
329 return array;
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346 public static List<Boolean> asList(boolean... backingArray) {
347 if (backingArray.length == 0) {
348 return Collections.emptyList();
349 }
350 return new BooleanArrayAsList(backingArray);
351 }
352
353 @GwtCompatible
354 private static class BooleanArrayAsList extends AbstractList<Boolean>
355 implements RandomAccess, Serializable {
356 final boolean[] array;
357 final int start;
358 final int end;
359
360 BooleanArrayAsList(boolean[] array) {
361 this(array, 0, array.length);
362 }
363
364 BooleanArrayAsList(boolean[] array, int start, int end) {
365 this.array = array;
366 this.start = start;
367 this.end = end;
368 }
369
370 @Override public int size() {
371 return end - start;
372 }
373
374 @Override public boolean isEmpty() {
375 return false;
376 }
377
378 @Override public Boolean get(int index) {
379 checkElementIndex(index, size());
380 return array[start + index];
381 }
382
383 @Override public boolean contains(Object target) {
384
385 return (target instanceof Boolean)
386 && Booleans.indexOf(array, (Boolean) target, start, end) != -1;
387 }
388
389 @Override public int indexOf(Object target) {
390
391 if (target instanceof Boolean) {
392 int i = Booleans.indexOf(array, (Boolean) target, start, end);
393 if (i >= 0) {
394 return i - start;
395 }
396 }
397 return -1;
398 }
399
400 @Override public int lastIndexOf(Object target) {
401
402 if (target instanceof Boolean) {
403 int i = Booleans.lastIndexOf(array, (Boolean) target, start, end);
404 if (i >= 0) {
405 return i - start;
406 }
407 }
408 return -1;
409 }
410
411 @Override public Boolean set(int index, Boolean element) {
412 checkElementIndex(index, size());
413 boolean oldValue = array[start + index];
414
415 array[start + index] = checkNotNull(element);
416 return oldValue;
417 }
418
419 @Override public List<Boolean> subList(int fromIndex, int toIndex) {
420 int size = size();
421 checkPositionIndexes(fromIndex, toIndex, size);
422 if (fromIndex == toIndex) {
423 return Collections.emptyList();
424 }
425 return new BooleanArrayAsList(array, start + fromIndex, start + toIndex);
426 }
427
428 @Override public boolean equals(Object object) {
429 if (object == this) {
430 return true;
431 }
432 if (object instanceof BooleanArrayAsList) {
433 BooleanArrayAsList that = (BooleanArrayAsList) object;
434 int size = size();
435 if (that.size() != size) {
436 return false;
437 }
438 for (int i = 0; i < size; i++) {
439 if (array[start + i] != that.array[that.start + i]) {
440 return false;
441 }
442 }
443 return true;
444 }
445 return super.equals(object);
446 }
447
448 @Override public int hashCode() {
449 int result = 1;
450 for (int i = start; i < end; i++) {
451 result = 31 * result + Booleans.hashCode(array[i]);
452 }
453 return result;
454 }
455
456 @Override public String toString() {
457 StringBuilder builder = new StringBuilder(size() * 7);
458 builder.append(array[start] ? "[true" : "[false");
459 for (int i = start + 1; i < end; i++) {
460 builder.append(array[i] ? ", true" : ", false");
461 }
462 return builder.append(']').toString();
463 }
464
465 boolean[] toBooleanArray() {
466
467 int size = size();
468 boolean[] result = new boolean[size];
469 System.arraycopy(array, start, result, 0, size);
470 return result;
471 }
472
473 private static final long serialVersionUID = 0;
474 }
475
476
477
478
479
480
481 @Beta
482 public static int countTrue(boolean... values) {
483 int count = 0;
484 for (boolean value : values) {
485 if (value) {
486 count++;
487 }
488 }
489 return count;
490 }
491 }